using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UI_HealthBar : MonoBehaviour
{
    private Entity entity;
    private CharacterStats myStats;
    private RectTransform myTransform;//UI的Transform类型
    private Slider slider;//hp滑动条
    private void Start()
    {
        entity = GetComponentInParent<Entity>();
        myTransform = GetComponent<RectTransform>();
        slider = GetComponentInChildren<Slider>();
        myStats = GetComponentInParent<CharacterStats>();

        entity.onFlipped += FlipUI;//翻转事件触发
        myStats.onHealthChanged += UpdateHealthUI;//HP改变事件触发
        UpdateHealthUI();
    }

    //人物翻转不翻转UI
    private void FlipUI()
    {
        myTransform.Rotate(0, 180, 0);
    }
    //HP UI组件消失取消订阅事件
    private void OnDisable()
    {
        entity.onFlipped -= FlipUI;
        myStats.onHealthChanged -= UpdateHealthUI;
    }
    //HP的UI更新
    private void UpdateHealthUI()
    {
        slider.maxValue = myStats.GetMaxHealthValue();
        slider.value = myStats.currentHealth;
    }
}
